(function (vc) {
    vc.extends({
        data: {
            addHousekeepingServInfo: {
                servId: '',
                servName: '',
                servDesc: '',
                hktId: '',
                sales: '',
                sort: '',
                state: '',
                states: [],
                repairWay: '',
                returnVisitFlag: '',
                keyword: '',
                coverPhoto: '',
                context: '',
                areaCode: '',
                carouselFigurePhoto: [],
                housekeepingTypes: [],
                repairWays: [],
                states: [],
                returnVisitFlags: []
            }
        },
        _initMethod: function () {
            $that._initAddHousekeepingServ();
            $that._listAddHousekeepingTypes();
            vc.getDict('housekeeping_serv', "repair_way", function (_data) {
                $that.addHousekeepingServInfo.repairWays = _data;
            });
            vc.getDict('housekeeping_serv', "state", function (_data) {
                $that.addHousekeepingServInfo.states = _data;
            });
            vc.getDict('housekeeping_serv', "return_visit_flag", function (_data) {
                $that.addHousekeepingServInfo.returnVisitFlags = _data;
            });
        },
        _initEvent: function () {
            vc.on("addHousekeepingServ", "notifyUploadCoverImage", function (_param) {
                if (_param.length > 0) {
                    $that.addHousekeepingServInfo.coverPhoto = _param[0].url;;
                } else {
                    $that.addHousekeepingServInfo.coverPhoto = '';
                }
            });
            vc.on("addHousekeepingServ", "notifyUploadCarouselFigureImage", function (_param) {
                $that.addHousekeepingServInfo.carouselFigurePhoto = [];
                _param.forEach(_p => {
                    $that.addHousekeepingServInfo.carouselFigurePhoto.push(_p.url);
                });
            });
        },
        methods: {
            addHousekeepingServValidate() {
                return vc.validate.validate({
                    addHousekeepingServInfo: $that.addHousekeepingServInfo
                }, {
                    'addHousekeepingServInfo.servName': [{
                        limit: "required",
                        param: "",
                        errInfo: "服务名称不能为空"
                    },
                        {
                            limit: "maxLength",
                            param: "30",
                            errInfo: "服务名称太长"
                        },
                    ],
                    'addHousekeepingServInfo.servDesc': [{
                        limit: "maxLength",
                        param: "256",
                        errInfo: "物品名称太长"
                    },],
                    'addHousekeepingServInfo.hktId': [{
                        limit: "required",
                        param: "",
                        errInfo: "服务类型不能为空"
                    },
                        {
                            limit: "maxLength",
                            param: "30",
                            errInfo: "必填，服务类型太长"
                        },
                    ],
                    'addHousekeepingServInfo.keyword': [{
                        limit: "required",
                        param: "",
                        errInfo: "关键字不能为空"
                    }],
                    'addHousekeepingServInfo.sales': [{
                        limit: "required",
                        param: "",
                        errInfo: "销量不能为空"
                    },
                        {
                            limit: "num",
                            param: "",
                            errInfo: "销量不是有效数字"
                        },
                    ],
                    'addHousekeepingServInfo.repairWay': [{
                        limit: "required",
                        param: "",
                        errInfo: "派单方式不能为空"
                    }],
                    'addHousekeepingServInfo.returnVisitFlag': [{
                        limit: "required",
                        param: "",
                        errInfo: "回访方式不能为空"
                    }],
                    'addHousekeepingServInfo.sort': [{
                        limit: "required",
                        param: "",
                        errInfo: "排序不能为空"
                    },
                        {
                            limit: "num",
                            param: "",
                            errInfo: "排序不是有效数字"
                        },
                    ],
                    'addHousekeepingServInfo.state': [{
                        limit: "required",
                        param: "",
                        errInfo: "上架状态不能为空"
                    },]
                });
            },
            saveHousekeepingServInfo: function () {
                if (!$that.addHousekeepingServValidate()) {
                    vc.toast(vc.validate.errInfo);
                    return;
                }
                $that.addHousekeepingServInfo.shopId = vc.getCurrentCommunity().shopId;
                $that.addHousekeepingServInfo.areaCode = vc.getCurrentCommunity().areaCode;
                vc.http.apiPost(
                    '/housekeepingServ.saveHousekeepingServ',
                    JSON.stringify($that.addHousekeepingServInfo), {
                        emulateJSON: true
                    },
                    function (json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            //关闭model
                            vc.toast("添加成功");
                            vc.goBack();
                            return;
                        }
                        vc.toast(_json.msg);
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    });
            },
            _listAddHousekeepingTypes: function () {
                let param = {
                    params: {
                        page: 1,
                        row: 50,
                        typeCd: 1001,
                        shopId: vc.getCurrentCommunity().shopId
                    }
                };
                //发送get请求
                vc.http.apiGet('/housekeepingType.queryHousekeepingType',
                    param,
                    function (json, res) {
                        let _json = JSON.parse(json);
                        $that.addHousekeepingServInfo.housekeepingTypes = _json.data;
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            _initAddHousekeepingServ: function () {
                let $summernote = $('.addSummernote').summernote({
                    lang: 'zh-CN',
                    height: 300,
                    placeholder: '必填，请输入服务描述',
                    callbacks: {
                        onImageUpload: function (files, editor, $editable) {
                            $that.sendFile($summernote, files);
                        },
                        onChange: function (contexts, $editable) {
                            $that.addHousekeepingServInfo.context = contexts;
                        }
                    },
                    toolbar: [
                        ['style', ['style']],
                        ['font', ['bold', 'italic', 'underline', 'clear']],
                        ['fontname', ['fontname']],
                        ['color', ['color']],
                        ['para', ['ul', 'ol', 'paragraph']],
                        ['height', ['height']],
                        ['table', ['table']],
                        ['insert', ['link', 'picture']],
                        ['view', ['fullscreen', 'codeview']],
                        ['help', ['help']]
                    ],
                });
            },
            sendFile: function ($summernote, files) {
                console.log('上传图片', files);
                var param = new FormData();
                param.append("uploadFile", files[0]);
                param.append('shopId', vc.getCurrentCommunity().shopId);
                vc.http.upload('/upload/uploadFile',
                    param, {
                        emulateJSON: true,
                        //添加请求头
                        headers: {
                            "Content-Type": "multipart/form-data"
                        }
                    },
                    function (json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            let data = _json.data;
                            //关闭model
                            $summernote.summernote('insertImage', data.url);
                            return;
                        }
                        vc.toast(json);
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    });
            },
        }
    });
})(window.vc);